package cn.xfdzx.android.apps.shop.adapter;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import cn.xfdzx.android.apps.shop.R;
import cn.xfdzx.android.apps.shop.activity.XFDShopDetailsActivity;
import cn.xfdzx.android.apps.shop.bean.ShopCarListBeen;
import cn.xfdzx.android.apps.shop.contract.ItemClickListener;
import cn.xfdzx.android.apps.shop.presenter.ShopCarListPresenter;
import cn.xfdzx.android.apps.shop.util.Utils;

/**
 * @author gzk
 * @date 2020/4/13 0013
 * description：购物车 批发
 */
public class CarWholesaleAdapter extends RecyclerView.Adapter<CarWholesaleAdapter.MyViewHolder> {

    private ItemClickListener mItemClickListener;
    private List<ShopCarListBeen.DataBean.ValidBean> mList;
    private Context mContext;
    private LayoutInflater mLayoutInflater;
    private int index;
    private TextView tvAllSelect;
    private TextView tvTotalMonry;
    private Dialog dialog;
    private ShopCarListPresenter mPresenter;

    public CarWholesaleAdapter(Context context, List<ShopCarListBeen.DataBean.ValidBean> mList, ShopCarListPresenter presenter) {
        this.mList = mList;
        this.mContext = context;
        this.mPresenter = presenter;
        mLayoutInflater = LayoutInflater.from(mContext);
    }

    public void setIndex(int index, TextView tvAddSelect, TextView tvTotalMonry ) {
        this.index = index;
        this.tvAllSelect = tvAddSelect;
        this.tvTotalMonry = tvTotalMonry;
    }

    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View inflate = mLayoutInflater.inflate(R.layout.item_car_wholesale, parent, false);

        MyViewHolder myHolder = new MyViewHolder(inflate);

        return myHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull final MyViewHolder holder, final int position) {
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(mContext);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        holder.rvCarWholesaleGoods.setLayoutManager(linearLayoutManager);
        holder.rvCarWholesaleGoods.setItemAnimator(new DefaultItemAnimator());
        holder.rvCarWholesaleGoods.setNestedScrollingEnabled(false);
        holder.rvCarWholesaleGoods.setFocusableInTouchMode(false);
        CarWholesaleGoodsAdapter carWholesaleGoodsAdapter;

        if (position < index) {
            holder.rlTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (Utils.isFastClick()) {
                        Intent intent = new Intent(mContext, XFDShopDetailsActivity.class);
                        intent.putExtra("shop_id", mList.get(position).getStore().getId() + "");
                        mContext.startActivity(intent);
                    }
                }
            });
            holder.tvShopName.setText(mList.get(position).getStore().getName());
            holder.ivShopSelect.setVisibility(View.VISIBLE);
//            holder.tvRemoveGood.setVisibility(View.INVISIBLE);
            carWholesaleGoodsAdapter = new CarWholesaleGoodsAdapter(mContext, mList.get(position).getGoods(), false, this, mPresenter, position);
        } else {
            holder.rlTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {

                }
            });
            holder.tvShopName.setText("失效商品");
            holder.ivShopSelect.setVisibility(View.INVISIBLE);
//            holder.tvRemoveGood.setVisibility(View.VISIBLE);
            carWholesaleGoodsAdapter = new CarWholesaleGoodsAdapter(mContext, mList.get(position).getGoods(), true, this, mPresenter, position);

        }

        holder.tvRemoveGood.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                    dialog = Utils.addDialog(mContext, R.layout.dialog, R.string.are_you_sure_you_want_to_clear_out_dead_items, R.string.confirm, new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {

                            String ids = "";
                            for (ShopCarListBeen.DataBean.ValidBean.GoodsBeanX goodsBeanX : mList.get(position).getGoods()) {
                                if (ids.isEmpty()) {
                                    ids = goodsBeanX.getId() + "";
                                } else {
                                    ids = ids + "," + goodsBeanX.getId();
                                }
                            }
                            mPresenter.removeGood(ids + "", position, 0, true);
                            dialog.dismiss();
                            index--;
                        }
                    });

                dialog.show();
             }
        });
        holder.rvCarWholesaleGoods.setAdapter(carWholesaleGoodsAdapter);

//        holder.itemView.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                mItemClickListener.onItemClick(position);
//            }
//        });

        mList.get(position).setIs_all_select(true);

        for (ShopCarListBeen.DataBean.ValidBean.GoodsBeanX x : mList.get(position).getGoods()) {
            if (!x.isIs_select()) {
                mList.get(position).setIs_all_select(false);
            }
        }

        if (mList.get(position).isIs_all_select()) {
            mList.get(position).setIs_select(true);
            holder.ivShopSelect.setImageResource(R.drawable.icon_green_dui);
        } else {
            mList.get(position).setIs_select(false);
            holder.ivShopSelect.setImageResource(R.drawable.d99999_oval);
        }

        holder.ivShopSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isFastClick()) {
                    if (mList.get(position).isIs_select()) {
                        mList.get(position).setIs_select(false);
                        mList.get(position).setIs_all_select(false);
                        holder.ivShopSelect.setImageResource(R.drawable.d99999_oval);
                        for (ShopCarListBeen.DataBean.ValidBean.GoodsBeanX x : mList.get(position).getGoods()) {
                            x.setIs_select(false);
                        }
                    } else {
                        holder.ivShopSelect.setImageResource(R.drawable.icon_green_dui);
                        mList.get(position).setIs_select(true);
                        mList.get(position).setIs_all_select(true);
                        for (ShopCarListBeen.DataBean.ValidBean.GoodsBeanX x : mList.get(position).getGoods()) {
                            x.setIs_select(true);
                        }
                    }
                    notifyDataSetChanged();
                    total();
                }
            }
        });


    }

    public void total() {
        notifyDataSetChanged();
        double total_money = 0;
        boolean is_all = true;
        for (int i = 0; i < index; i++) {
            for (ShopCarListBeen.DataBean.ValidBean.GoodsBeanX goodsBeanX : mList.get(i).getGoods()) {
                if (goodsBeanX.isIs_select()) {
                    total_money = Utils.addDouble(Utils.mul(goodsBeanX.getCart_num(), Double.parseDouble(goodsBeanX.getProductInfo().getPrice())), total_money);
                } else {
                    is_all = false;
                }
            }

        }
        if (is_all) {
            Drawable nav_up = mContext.getResources().getDrawable(R.drawable.icon_green_dui);
            nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
            tvAllSelect.setCompoundDrawables(nav_up, null, null, null);
        } else {
            Drawable nav_up = mContext.getResources().getDrawable(R.drawable.d99999_oval);
            nav_up.setBounds(0, 0, nav_up.getMinimumWidth(), nav_up.getMinimumHeight());
            tvAllSelect.setCompoundDrawables(nav_up, null, null, null);
        }

        tvTotalMonry.setText("¥" + total_money);

    }


    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {


        @BindView(R.id.rv_car_wholesale_goods)
        RecyclerView rvCarWholesaleGoods;
        @BindView(R.id.tv_shop_name)
        TextView tvShopName;
        @BindView(R.id.iv_shop_select)
        ImageView ivShopSelect;
        @BindView(R.id.rl_title)
        RelativeLayout rlTitle;
        @BindView(R.id.tv_remove_good)
        TextView tvRemoveGood;


        public MyViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);

        }
    }


    public void setOnItemClickListener(ItemClickListener itemClickListener) {
        this.mItemClickListener = itemClickListener;

    }

}
